<template>
  <el-card>
    <el-divider content-position="center">RSAKEY</el-divider>
    <el-card>
      <el-row>
        <el-col :span="24">
          <el-button type="warning" @click="logout" style="width: 100%;margin-bottom: 10px;">注销</el-button>
        </el-col>
        <el-col :span="24">
          <el-button type="primary" @click="publicKey" style="width: 100%;">生成公钥</el-button>
        </el-col>
        <el-col :span="24">
          <pre>{{ key.PUBLIC_KEY }}</pre>
        </el-col>
      </el-row>
    </el-card>

    <el-divider content-position="center">LOGIN</el-divider>
    <el-card style="width: 360px;margin: 0 auto;">
      <template #header>
        <span>登录</span>
      </template>
      <el-form ref="form" :model="loginForm" label-width="80px">
        <el-form-item label="用户名">
          <el-input type="text" v-model="loginForm.userName"></el-input>
        </el-form-item>
        <el-form-item label="密码">
          <el-input type="password" v-model="loginForm.password"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="login">登录</el-button>
        </el-form-item>
      </el-form>
    </el-card>

    <el-divider content-position="center">REGISTER</el-divider>
    <el-card style="width: 360px;margin: 0 auto;">
      <template #header>
        <span>注册</span>
      </template>
      <el-form ref="form" :model="registerForm" label-width="80px">
        <el-form-item label="用户名">
          <el-input type="text" v-model="registerForm.userName"></el-input>
        </el-form-item>
        <el-form-item label="密码">
          <el-input type="password" v-model="registerForm.password"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="register">注册</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </el-card>

</template>

<script setup>
import {reactive, getCurrentInstance} from "vue";
import {getPublicKey, userLogin, userLogout, userRegister} from "../assets/api";
import JSEncrypt from 'jsencrypt';
import {Base64} from "js-base64";

const {proxy} = getCurrentInstance();

let key = reactive({
  PUBLIC_KEY: ''
});

// 登录
const loginForm = reactive({
  userName: 'Admin',
  password: 'Lee123456@@@'
});

// 获取公钥
const publicKey = () => {
  getPublicKey().then(({data}) => {
    localStorage.setItem('PUBLIC_KEY', data.PUBLIC_KEY)
    key.PUBLIC_KEY = data.PUBLIC_KEY;
  })
}

// 登录
const login = () => {
  let encryptor = new JSEncrypt();
  encryptor.setPublicKey(key.PUBLIC_KEY);
  let encrypted = encryptor.encrypt(JSON.stringify(loginForm));
  userLogin({loginParams: encrypted}).then(({data}) => {
    localStorage.setItem('userInfo', JSON.stringify(data));
    proxy.$router.push('/page');
  })
}

// 注册
const registerForm = reactive({
  userName: '',
  password: ''
});

// 注册
const register = () => {
  let encryptor = new JSEncrypt();
  encryptor.setPublicKey(key.PUBLIC_KEY);
  let encrypted = encryptor.encrypt(JSON.stringify(registerForm));
  userRegister({registerParams: encrypted}).then(({data}) => {
    proxy.$router.push('/login')
  })
}

// 注销
const logout = () => {
  userLogout().then(({data}) => {
    proxy.$router.push('/login');
  })
}

</script>

<style scoped>

</style>
